package cn.org.queue.receive;

import cn.org.queue.manage.RocketQueueManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

import static cn.org.queue.handler.ReceiveMessageHandler.decodeRocketMessage;

/**
 * @Package: cn.org.queue.receive
 * @ClassName: RocketConsumerMessage
 * @Description:
 * @Author: mac-pro
 * @CreateDate: 2019/2/18 下午4:58
 * @Version: 1.0
 */
@Slf4j
public class RocketConsumerMessage extends RocketQueueManager {
    String topic;
    public RocketConsumerMessage(String topic) {
        super(topic);
        this.topic = topic;
    }
    public void receiveMessage() {
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            /**
             * 默认msgs里只有一条消息，可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
             */
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExtList, ConsumeConcurrentlyContext context) {
                decodeRocketMessage(messageExtList,topic);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}
